import { _decorator, Button, Component, tween, Tween, UITransform, Vec3, Widget } from 'cc';
import { DataProxy } from '../../../frame/A-LIB/sources/lib.b.data';
import { AudioManager } from '../../../frame/component/audio-manager';
import GameDC from '../../data/data.game.center';
import DataManager from '../../manager/data.manager';
import { Popup } from './pop-up';
const { ccclass, property } = _decorator;

@ccclass('TreasureBox')
export class TreasureBox extends Component {
	@property(Button)
	private button: Button = null;

	private ui_btn: UITransform;
	private va_btn: mtec.cc.VecAssist;

	private ui_status: 'show'|'hide';
	private flying: boolean;
	private fly_path: Array<Vec3>;
	private fly_target: number;

	private interval = 60;
	private speed: number;
	private calculate_vec: Vec3;

	protected onLoad(){
		this.init();
		this.listening();
	}

	protected start(){
		this.adaptUI();
		this.initUIStatus();
	}

	private init(){
		this.button = this.node.getChildByName('button').getComponent(Button);
		this.ui_btn = this.button.getComponent(UITransform);
		this.va_btn = new mtec.cc.VecAssist(this.button.node);

		this.calculate_vec = new Vec3();
		this.fly_path = mtec.array.create(7, ()=>new Vec3());
	}

	private adaptUI(){
		let wgt = this.node.getComponent(Widget);
		if(!wgt){
			wgt = this.node.addComponent(Widget);
		}
		wgt.enabled = true;

		[wgt.isAlignTop, wgt.isAlignBottom, wgt.isAlignLeft, wgt.isAlignRight] = [true, true, true, true];
		[wgt.isAbsoluteTop, wgt.isAbsoluteBottom, wgt.isAbsoluteLeft, wgt.isAbsoluteRight] = [true, true, true, true];
		wgt.top = mtec.cc.is_long_screen ? 480 : 380;
		wgt.bottom = 460;
		wgt.left = 0; wgt.right = 0;

		wgt.updateAlignment();
	}

	private initUIStatus(){
		this.speed = this.node.getComponent(UITransform).width / 6;
		let show = !!GameDC.Trigger.UILuckyBox;
		if(show){
			this.show();
		}else if(!show){
			this.hide(false);
		}
	}

	private listening(){
		DataProxy.follow(GameDC.Trigger, 'UILuckyBox', (o, v)=>this.onUILuckyBoxTrigger(v, o), false);
	}

	private onClick(){
		DataManager.userPoint('ClickLuckyBox');
		AudioManager.playEffect(AudioManager.EAudio.EF_CLICK);
		this.pause();
		Popup.Treasurebox().then(reap=>{
			if(reap){
				DataManager.userPoint('GetLuckyBox');
				DataManager.changeDiamond(3);
				this.hide(true);
			}else{
				this.resume();
			}
		});
	}

	private onUILuckyBoxTrigger(show: boolean, ov: boolean){
		if(show){
			this.show();
		}else{
			this.hide(false);
		}
	}

	private show(){
		if(this.ui_status=='show'){
			if(!this.flying){
				this.resume();
			}
			return void 0;
		}
		this.ui_status = 'show';

		this.node.active = true;
		this.button.node.active = true;
		Tween.stopAllByTarget(this.button.node);

		this.flying = true;
		this.refreshFlyPath();
		this.button.node.setPosition(this.fly_path[0]);
		this.next();
	}

	private refreshFlyPath(){
		let {top, bottom, right, left} = this.va_btn;
		this.fly_path.forEach((vec, i)=>{
			vec.x = (i%2)==0 ? left : right;
			vec.y = mtec.number.random(top, bottom, 2);
			vec.z = this.button.node.position.z;
		});

		this.fly_path[0].x = this.va_btn.over_left;
		this.fly_path.lastElement.x = this.va_btn.over_left;
		this.fly_target = 0;
	}

	private next(){
		this.fly_target++;
		Tween.stopAllByTarget(this.button.node);

		if(this.fly_target>=this.fly_path.length){
			this.hide(true);
		}else{
			let time = Vec3.subtract(this.calculate_vec, this.fly_path[this.fly_target], this.button.node.position).length() / this.speed;
			tween(this.button.node)
			.to(time, {position: this.fly_path[this.fly_target]})
			.call(()=>this.next()).start();
		}
	}

	private hide(next: boolean){
		if(this.ui_status=='hide'){
			return void 0;
		}
		this.ui_status = 'hide';

		this.pause();
		this.button.node.active = false;

		if(next){
			mtec.delay(this.interval, ()=>{
				if(this.node.active){
					this.show();
				}
			});
		}else{
			this.node.active = false;
		}
	}

	private pause(){
		if(!this.flying){
			return void 0;
		}
		this.flying = false;
		Tween.stopAllByTarget(this.button.node);
	}

	private resume(){
		if(this.flying){
			return void 0;
		}
		this.fly_target--;

		this.flying = true;
		this.next();
	}
}

